﻿Imports System.Web.Script.Serialization

Public Class JsonHelper

    ''' <summary>
    ''' 将对象转换成Json字符串
    ''' </summary>
    ''' <param name="obj">要转换的对象</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function ToJson(ByVal obj As Object) As String
        Try
            Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
            Return serializer.Serialize(obj)
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
        Return ""
    End Function

    ' 定义一个泛型方法来将 JSON 字符串转换为对象
    Public Shared Function DeserializeJson(Of T)(jsonString As String) As T
        Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
        Return serializer.Deserialize(Of T)(jsonString)
    End Function

End Class